package com.senlingspace.system.service.impl;

import java.util.List;

import com.senlingspace.common.exception.UtilException;
import com.senlingspace.common.utils.DateUtils;
import com.senlingspace.common.utils.SecurityUtils;
import com.senlingspace.system.domain.SlkjShopOperate;
import com.senlingspace.system.domain.dto.FzShopInvestorDto;
import com.senlingspace.system.domain.vo.SharesInvestorVo;
import com.senlingspace.system.mapper.SlkjShopOperateMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.senlingspace.system.mapper.SlkjShopInvestorMapper;
import com.senlingspace.system.domain.SlkjShopInvestor;
import com.senlingspace.system.service.ISlkjShopInvestorService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 分账比例-门店投资方关系Service业务层处理
 * 
 * @author SenlingSpace
 * @date 2024-07-15
 */
@Service
public class SlkjShopInvestorServiceImpl implements ISlkjShopInvestorService 
{
    @Autowired
    private SlkjShopInvestorMapper slkjShopInvestorMapper;
    @Autowired
    private SlkjShopOperateMapper operateMapper;

    /**
     * 查询分账比例-门店投资方关系
     * 
     * @param id 分账比例-门店投资方关系主键
     * @return 分账比例-门店投资方关系
     */
    @Override
    public SlkjShopInvestor selectSlkjShopInvestorById(Long id)
    {
        return slkjShopInvestorMapper.selectSlkjShopInvestorById(id);
    }

    /**
     * 查询分账比例-门店投资方关系列表
     * 
     * @param slkjShopInvestor 分账比例-门店投资方关系
     * @return 分账比例-门店投资方关系
     */
    @Override
    public List<SlkjShopInvestor> selectSlkjShopInvestorList(SlkjShopInvestor slkjShopInvestor)
    {
        return slkjShopInvestorMapper.selectSlkjShopInvestorList(slkjShopInvestor);
    }

    /**
     * 新增分账比例-门店投资方关系
     * 
     * @param slkjShopInvestor 分账比例-门店投资方关系
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int insertSlkjShopInvestor(List<SlkjShopInvestor> slkjShopInvestor)
    {
        int i = 0;
        for(SlkjShopInvestor investor : slkjShopInvestor){
            int countInvestor = slkjShopInvestorMapper.selectFzShopInvestor(investor.getShopId(), investor.getInvestorId());
            if(countInvestor > 0) {
                throw new IllegalArgumentException("投资方已存在");
            }
            int commitNum = slkjShopInvestorMapper.insertSlkjShopInvestor(investor);
            i = i+commitNum;
        }
        return i;
    }

    /**
     * 修改分账比例-门店投资方关系
     * 
     * @param shopInvestorDto 分账比例-门店投资方关系
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateSlkjShopInvestor(FzShopInvestorDto shopInvestorDto)
    {
        shopInvestorDto.setUpdateTime(DateUtils.getNowDate());
        shopInvestorDto.setUpdateBy(SecurityUtils.getUsername());
        int InvestorNum = slkjShopInvestorMapper.updateSlkjShopInvestor(shopInvestorDto);
        SlkjShopOperate slkjShopOperate = new SlkjShopOperate();
        slkjShopOperate.setId(shopInvestorDto.getOperateId());
        slkjShopOperate.setInvestorBefore(shopInvestorDto.getOperateBefore());
        slkjShopOperate.setInvestorAfter(shopInvestorDto.getOperateAfter());
        slkjShopOperate.setUpdateBy(SecurityUtils.getUsername());
        slkjShopOperate.setUpdateTime(DateUtils.getNowDate());
        int operateNum = operateMapper.updateSlkjShopOperate(slkjShopOperate);
        if(operateNum == 0 || InvestorNum == 0 ){
            throw new UtilException("修改数据失败");
        }
        return InvestorNum;
    }

    // /**
    //  * 批量删除分账比例-门店投资方关系
    //  *
    //  * @param ids 需要删除的分账比例-门店投资方关系主键
    //  * @return 结果
    //  */
    // @Override
    // public int deleteSlkjShopInvestorByIds(Long[] ids)
    // {
    //     return slkjShopInvestorMapper.deleteSlkjShopInvestorByIds(ids);
    // }
    //
     /**
      * 删除分账比例-门店投资方关系信息
      *
      * @param shopInvestorDto 分账比例-门店投资方关系
      * @return 结果
      */
     @Override
     @Transactional(rollbackFor = Exception.class)
     public int deleteSlkjShopInvestorById(FzShopInvestorDto shopInvestorDto)
     {
         int InvestorNum = slkjShopInvestorMapper.deleteSlkjShopInvestorById(shopInvestorDto.getId());
         SlkjShopOperate slkjShopOperate = new SlkjShopOperate();
         slkjShopOperate.setId(shopInvestorDto.getOperateId());
         slkjShopOperate.setInvestorBefore(shopInvestorDto.getInvestorBefore());
         slkjShopOperate.setInvestorAfter(shopInvestorDto.getInvestorAfter());
         slkjShopOperate.setUpdateBy(SecurityUtils.getUsername());
         slkjShopOperate.setUpdateTime(DateUtils.getNowDate());
         int operateNum = operateMapper.updateSlkjShopOperateAdd(slkjShopOperate);
         if(operateNum == 0 || InvestorNum == 0 ){
             throw new UtilException("修改数据失败");
         }
         return operateNum;
     }

    @Override
    public List<SharesInvestorVo> selectInvestorShopByShopId(Long shopId) {

        List<SharesInvestorVo> sharesInvestorVos = slkjShopInvestorMapper.selectInvestorShopByShopId(shopId);
        return sharesInvestorVos;
    }
}
